package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.bo.spuorder.SpuOrderBo;
import org.summer.lawyer.domain.bo.spuorder.SpuOrderTestBo;
import org.summer.lawyer.domain.bo.spuorderassign.SpuOrderAssignBo;
import org.summer.lawyer.domain.vo.spuorder.SpuOrderExport;
import org.summer.lawyer.domain.vo.spuorder.SpuOrderVo;
import org.summer.lawyer.domain.vo.spuorderassign.SpuOrderAssignVo;
import org.summer.lawyer.service.ISpuOrderAssignService;
import org.summer.lawyer.service.ISpuOrderService;

import java.util.List;

/**
 * 订单信息
 *
 * @author summer
 * @date 2024-06-21
 */
@Tag(name = "后台相关接口", description = "订单信息")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/spuOrder")
public class SpuOrderController extends BaseController {

    private final ISpuOrderService spuOrderService;
    private final ISpuOrderAssignService spuOrderAssignService;

    /**
     * 查询订单信息列表
     */
    @Operation(summary = "查询订单信息列表接口", description = "查询订单信息列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("trade:order:list")
    @GetMapping("/list")
    public TableDataInfo<SpuOrderVo> list(SpuOrderBo bo, PageQuery pageQuery) {
        return spuOrderService.selectPageList(bo, pageQuery);
    }

    /**
     * 查询派单信息列表
     */
    @SaCheckPermission("trade:order:list")
    @GetMapping("/listAssignOrder")
    public TableDataInfo<SpuOrderAssignVo> listAssignOrder(SpuOrderAssignBo bo, PageQuery pageQuery) {
        bo.setActive("1");
        bo.setPayed("1");
        return spuOrderAssignService.selectPageLawFirmList(bo,pageQuery);
    }

    /**
     * 导出订单信息列表
     */
    @Operation(summary = "导出订单信息列表接口", description = "导出订单信息列表")
    @SaCheckPermission("trade:order:export")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SpuOrderAssignBo bo, HttpServletResponse response) {
        List<SpuOrderAssignVo> list = spuOrderAssignService.selectSpuOrderAssignLawFirmList(bo);
        List<SpuOrderExport> exportList = MapstructUtils.convert(list, SpuOrderExport.class);
        for (SpuOrderExport spuOrderExport : exportList) {
            //转换为元
            spuOrderExport.setAmount( spuOrderExport.getAmount() / 100);
        }
        ExcelUtil.exportExcel(exportList, "订单信息", SpuOrderExport.class, response);
    }

    /**
     * 获取订单信息详细信息
     *
     * @param orderId 主键
     */
    @Operation(summary = "获取订单信息详细信息接口", description = "查询订单信息列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    //@SaCheckPermission("trade:order:query")
    @SaCheckLogin
    @GetMapping("/{orderId}")
    public R<SpuOrderAssignVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long orderId) {
       // return R.ok(spuOrderService.selectSpuOrderVoById(orderId));
        return R.ok(spuOrderAssignService.selectSpuOrderAssignVoBySpuOrderId(orderId));
    }

    /**
     * 新增订单信息
     */
    @Operation(summary = "新增订单信息接口", description = "新增订单信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("trade:order:add")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveSpuOrder")
    public R<Void> saveSpuOrder(@Validated(AddGroup.class) @RequestBody SpuOrderBo bo) {
        return toAjax(spuOrderService.insertByBo(bo));
    }

    /**
     * 修改订单信息
     */
    @Operation(summary = "修改订单信息接口", description = "修改订单信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("trade:order:edit")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateSpuOrder")
    public R<Void> updateSpuOrder(@Validated(EditGroup.class) @RequestBody SpuOrderBo bo) {
        return toAjax(spuOrderService.updateByBo(bo));
    }

    /**
     * 删除订单信息
     *
     * @param orderIds 主键串
     */
    @Operation(summary = "删除订单信息接口", description = "删除订单信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("trade:order:remove")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeSpuOrder/{orderIds}")
    public R<Void> removeSpuOrder(@NotEmpty(message = "主键不能为空")
                                  @PathVariable Long[] orderIds) {
        return toAjax(spuOrderService.deleteWithValidByIds(List.of(orderIds), true));
    }


    @Operation(summary = "撤回分派订单")
    @Log(title = "撤回分派订单", businessType = BusinessType.OTHER)
    @SaCheckPermission("trade:order:revoke")
    @PostMapping("/revokeOrder")
    public R<Void> listComment(@Validated @RequestBody SpuOrderBo bo){
        spuOrderService.revokeSpuOrder(bo);
        return R.ok();
    }

    /**
     * 分派订单
     */
    @Log(title = "分派订单", businessType = BusinessType.OTHER)
    @SaCheckPermission("trade:order:assign")
    @PostMapping("/assignOrder")
    public R<Void> assignOrder(@Validated @RequestBody SpuOrderBo bo){
        spuOrderService.assigningSpuOrder(bo);
        return R.ok();
    }

    /**
     * 订单测试
     */
    @SaIgnore
    @PostMapping("/createOrder")
    public R<Void> createOrder(@RequestBody SpuOrderTestBo bo){
        //创建订单信息
        spuOrderService.testOrder(bo);

        return R.ok();
    }
}
